<!--
 * Created by PhpStorm.
 * 版权所有: 2019~2022 [ hhygyl ]
 * Date: 2019/8/21-9:08
 * Link: http://luckyadmin.luckyhhy.cn
 * FileName: index.html
 *-->


<!-- 引入基类模板 -->
{extend name='public/base' /}


{block name="search"}

{:widget('forms/search',array('name','标题搜索','请输入名称'))}

{/block}

{block name="btn"}
{:widget('forms/Delbtn',array('删除'))}
<!--按钮区-->
{:widget('forms/Addbtn',array('新增模板',1,'add'))}
{:widget('forms/reload')}

{/block}

{block name="table"} <!--表格区-->

{:widget('forms/table')}

{/block}

{block name="js"} <!--js处理区-->



<!--模板-->
<script type="text/html" id="operationTpl">
    {:widget('forms/Editbtn',array('编辑模板'))}

    {:widget('forms/Delbtn',array('删除模板',2))}

</script>

<script type="text/html" id="status">
    <input type="checkbox" name="status"  lay-data="{{d.status}}" value="{{d.id}}"  lay-filter="sexDemo" lay-skin="switch"  lay-text="开启|关闭"  {{ d.status == 1 ? 'checked' : '' }}>
</script>



<script>
    layui.use(['element', 'table', 'form', 'jquery','lucky'], function () {
        var element = layui.element;
        var form = layui.form;
        var table = layui.table;
        var $ = layui.jquery;

        var lucky=layui.lucky;

        form.render();

        // layer.load(2);
        table.render({
            elem: '#tableFilter',
            url:"{:url('email/index')}",
            //toolbar: '#toolbarDemo',
            even: true, //开启隔行背景
            id:'table_id',
            page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip','last'] //自定义分页布局
                //,curr: 5 //设定初始在第 5 页
                ,groups: 5 //只显示 1 个连续页码
                ,first: false //不显示首页
                ,last: false //不显示尾页
                ,limit:20
                ,limits:[20,60,70,80,90,100]
            },
            //height: 'full-100',
            text: {
                none: '暂无相关数据'
            },
            cols: [[
                {type: 'checkbox'},
                {field: 'id',  title: 'ID',width:60,sort:true},
                {field: 'name', width: 200, title: '模板名称'},
                {field: 'tag', width: 100, title: '模板标识'},
                {field: 'content', minWidth: 200, title: '模板内容'},
                {field:'status',title:'是否启用',templet: '#status',width:150 ,unresize: true,sort:true},
                {field: 'time_formt', title: '添加时间',align: 'center',width:200},
                {fixed: 'right',templet: '#operationTpl', width: 100, align: 'center', title: '操作'}
            ]],

            done: function (res) {
                layer.closeAll('loading');
            }
        });

        /**
         * 表格搜索
         */
        form.on('submit(search)', function (obj) {
            lucky.CreateSearch("table_id",obj.field); //查询
            return false;
        });


        /**
         * 监听是否显示操作
         */
        form.on('switch(sexDemo)', function(obj){
            var _id=parseInt(obj.value);
            var pan=obj.elem.checked;
            var status;
            if (pan===false){
                status=2;
            }else {
                status=1;
            }
            lucky.Change_status("table_id","email_template",_id,"status",status, obj.othis);
        });


        /**
         * 监听单行工具操作
         */
        table.on('tool(tableFilter)', function (obj) {
            var data = obj.data;
            // console.log(JSON.stringify(data));
            var _id=parseInt(data.id);
            var layEvent = obj.event;
            if(layEvent==="edit"){
                var urls="/admin/email/EditTemplate/id/"+_id;
                lucky.CreateForm("编辑内容",'70%','80%',urls,"table_id");

            }  else if(layEvent==="del"){
                lucky.Delete_data(_id,"{:url('email/delete')}","table_id");
            }

        });


        var active = {
            add:function(){
                lucky.CreateForm("新增模板",'70%','80%',"{:url('email/AddTemplate')}","table_id");
            },
            del: function(){ //获取选中数据
                var checkStatus = table.checkStatus('table_id'),data = checkStatus.data;
                var num=0;
                var id=[];
                for (var i in data) {
                    num++;
                    id.push(data[i].id);
                }
                if (num<1) {
                    layer.msg("请选择一项",{time:1500});return false;
                }
                lucky.Delete_data(id,"{:url('email/delete')}","table_id");
            },
            reload: function(){
                lucky.CreateReload("table_id");
            },

        };

        $('.do_btn .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });

</script >


{/block}
